package jwdt.xadmin.finace.action;

import java.util.TreeMap;

import jwdt.xadmin.finace.bean.HaFl;
import jwdt.xadmin.finace.bean.HaFlmx;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller("/xadmin/finace")
@SuppressWarnings({"rawtypes", "unchecked"})
public class ZdAction extends FinaceAction {

	@RequestMapping("/zdlist")
	public Page doZdlist(Form f, Module m) throws Exception {
		// 
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", sys.getAdminnum());
		String sql = " order by px,fldm";
		String fldm = f.getString("fldm2");
		if(!"".equals(fldm)) {
			sql = " and fldm like '%" + fldm + "%'" + sql;
			f.addQueryString("fldm2");
		}
		String flmc = f.getString("flmc2");
		if (!"".equals(flmc)) {
			sql = " and flmc like '%" + flmc + "%'" + sql;
			f.addQueryString("flmc2");
		}
		int zt = f.getInt("zt2", -1);
		if(zt != -1) {
			sql = " zt=" + f.getInt("zt2", 1) + sql;
			f.addQueryString("zt2");
		} else {
			sql = " 1=1" + sql;
		}
		PageUtil pu = new PageUtil(HaFl.class, sql, page, pageSize);
		f.add("pager", pu.setUrl(m.getFullPath()+".do").setParams(f.buildQueryMap()));
		return m.defaultPage();
	}

	@RequestMapping("/zdadd")
	public Page doZdadd(Form f, Module m) throws Exception {
		return m.findPage(super.url+"zdadd");
	}

	@RequestMapping("/zdaddact")
	public Page doZdaddact(Form f, Module m) throws Exception {
		// 
		HaFl zd = (HaFl)f.copy2Class(HaFl.class);
		String ret = "添加成功";
		try {
			Jdb.get().add(zd);
		} catch (Exception e) {
			ret = "添加出错";
		}
		f.add("ret", ret);
		return doZdadd(f, m);
	}

	@RequestMapping("/zdedit")
	public Page doZdedit(Form f, Module m) throws Exception {
		// 
		String fldm = f.getString("fldm");
		f.add("obj", Jdb.get().get(HaFl.class, fldm));
		return m.defaultPage();
	}

	@RequestMapping("/zdeditact")
	public Page doZdeditact(Form f, Module m) throws Exception {
		// 
		HaFl zd = (HaFl)f.copy2Class(HaFl.class);
		String ret = "修改成功";
		try {
			Jdb.get().update(zd);
		} catch (Exception e) {
			ret = "修改出错";
		}
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "zdlist.do");
	}

	@RequestMapping("/zddel")
	public Page doZddel(Form f, Module m) throws Exception {
		// 
		String fldm = f.getString("fldm");
		HaFl fl = (HaFl)Jdb.get().get(HaFl.class, fldm);
		if(fl == null) {
			f.add("ret", "您要删除的记录不存在：ID=" + fldm);
			return m.findPage("tips");
		}
		String ret = "删除成功";
		try {
			Jdb.get().del(fl);
		} catch (Exception e) {
			ret = "删除出错";
		}
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "zdlist.do");
	}

	@RequestMapping("/zdmxlist")
	public Page doZdmxlist(Form f, Module m) throws Exception {
		// 
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", sys.getAdminnum());
		String fldm = f.getString("fldm", true);
		String sql = "fldm='" + fldm + "' order by px, mxdm";
		PageUtil pu = new PageUtil(HaFlmx.class, sql, page, pageSize);
		f.add("pager", pu.setUrl(m.getFullPath()+".do").setParams(f.buildQueryMap()));
		return m.defaultPage();
	}

	@RequestMapping("/zdmxadd")
	public Page doZdmxadd(Form f, Module m) throws Exception {
		return m.findPage(super.url+"zdmxadd");
	}
	
	@RequestMapping("/zdmxaddact")
	public Page doZdmxaddact(Form f, Module m) throws Exception {
		// 
		HaFlmx zdmx = (HaFlmx)f.copy2Class(HaFlmx.class);
		String ret = "添加成功";
		try {
			Jdb.get().add(zdmx);
		} catch (Exception e) {
			ret = "添加出错";
		}
		f.add("ret", ret);
		return doZdadd(f, m);
	}

	@RequestMapping("/zdmxedit")
	public Page doZdmxedit(Form f, Module m) throws Exception {
		// 
		String fldm = f.getString("fldm");
		String mxdm = f.getString("mxdm");
		TreeMap keys = new TreeMap();
		keys.put("fldm", fldm);
		keys.put("mxdm", mxdm);
		f.add("obj", Jdb.get().get(HaFlmx.class, keys));
		return m.defaultPage();
	}

	@RequestMapping("/zdmxeditact")
	public Page doZdmxeditact(Form f, Module m) throws Exception {
		// 
		HaFlmx zdmx = (HaFlmx)f.copy2Class(HaFlmx.class);
		String ret = "修改成功";
		try {
			Jdb.get().update(zdmx);
		} catch (Exception e) {
			ret = "修改出错";
		}
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "zdmxlist.do?fldm="+zdmx.getFldm());
	}

	@RequestMapping("/zdmxdel")
	public Page doZdmxdel(Form f, Module m) throws Exception {
		// 
		String fldm = f.getString("fldm");
		String mxdm = f.getString("mxdm");
		TreeMap keys = new TreeMap();
		keys.put("fldm", fldm);
		keys.put("mxdm", mxdm);
		HaFlmx zdmx = (HaFlmx)Jdb.get().get(HaFlmx.class, keys);
		if(zdmx == null) {
			f.add("ret", "您要删除的记录不存在：ID=" + fldm);
			return m.findPage("tips");
		}
		String ret = "删除成功";
		try {
			Jdb.get().del(zdmx);
		} catch (Exception e) {
			ret = "删除出错";
		}
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "zdmxlist.do?fldm="+zdmx.getFldm());
	}
	
}
